local jinwu = fk.CreateSkill{
  name = "iberia__jinwu"
}

Fk:loadTranslationTable{
  [jinwu.name] = "矜武",
  [":"..jinwu.name] = "出牌阶段开始时，你可以将一名角色的一张牌当【杀】使用，若为其他角色的牌，其可以为此牌增加一个目标或弃置你的两张牌。",

  [jinwu.name.."-slash"] = "矜武：将 %src 的一张牌当【杀】使用（不选牌则为其一张手牌）",
  [jinwu.name.."-borrow"] = "矜武：将一名角色的一张牌当【杀】，请选择一名角色",
  [jinwu.name.."-choose"] = "矜武：可以为 %arg 增加一个目标<br/>或选择 %src 并弃置其两张牌。",
  [jinwu.name.."-discard"] = "矜武：选择 %src 的两张手牌弃置。",
}

jinwu:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(jinwu.name) and player == target then
      return player.phase == Player.Play and player:canUse(Fk:cloneCard("slash"), {bypass_times = true})
      and table.find(player.room.alive_players, function (p) return not p:isNude() end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p) return not p:isNude() end)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      skill_name = jinwu.name,
      prompt = jinwu.name.."-borrow",
    })

    if #tos == 1 then
      event:setCostData(self, tos[1])
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local to = event:getCostData(self) ---@type ServerPlayer
    local room = player.room

    local visibles, unvisibles = {}, {}
    for _, c in ipairs(to:getCardIds("he")) do
      if player:cardVisible(c) then
        table.insert(visibles, c)
      else
        table.insert(unvisibles, c)
      end
    end

    local _, ret = room:askToUseActiveSkill(player, {
      skill_name = "iberia__viewas", cancelable = false,
      extra_data = { cards = visibles }
    })

    if ret then
      local card = Fk:cloneCard("slash")
      card.skillName = jinwu.name
      local cards = table.random(unvisibles, 1)
      if ret.cards and #ret.cards > 0 then cards = ret.cards end
      card:addSubcards(cards)
      room:useCard{
        card = card,
        from = player,
        tos = ret.targets,
        extra_data = { iberia__liyan_jinwu = to.id}
      }
    end
  end,
})

jinwu:addEffect(fk.TargetConfirming, {
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    return player == target and data.card.skillName == jinwu.name
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local from
    if data.extra_data then
      local id = data.extra_data.iberia__liyan_jinwu
      if id then from = room:getPlayerById(id) end
    end

    if not from then
      return false
    elseif from ~= player then
      local ex_targets = table.filter(room:getAlivePlayers(false), function(p)
        return player:canUseTo(data.card, p, {bypass_times = true})
      end)
      if player:getHandcardNum() >= 2 then
        table.insert(ex_targets, player)
      end

      local tos = room:askToChoosePlayers(from, {
        targets = ex_targets,
        min_num = 1,
        max_num = 1,
        skill_name = jinwu.name,
        cancelable = true,
        prompt = jinwu.name.."-choose:"..player.id.."::"..data.card:toLogString()
      })

      if #tos == 1 then
        event:setCostData(self, {from, tos[1]})
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local from, to = table.unpack{event:getCostData(self)} ---@type ServerPlayer, ServerPlayer
    local room = player.room
    if to == player then
      local cards = room:askToChooseCards(from, {
        target = player,
        flag = "he",
        min = 2,
        max = 2,
        skill_name = jinwu.name,
        prompt = jinwu.name.."-discard:"..player.id
      })
      room:throwCard(cards, jinwu.name, player, from)
    else
      data:addTarget(to)
    end
  end
})

return jinwu
